package com.jinlia.show.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinlia.show.common.core.exception.MyRuntimeException;
import com.jinlia.show.modules.base.constant.LabelEntityEnum;
import com.jinlia.show.modules.base.constant.i.IBaseEnum;
import com.jinlia.show.modules.base.model.po.GoodsPO;
import com.jinlia.show.modules.base.model.query.GoodsQuery;
import com.jinlia.show.modules.base.model.query.LabelQuery;
import com.jinlia.show.modules.base.model.vo.LabelVO;
import com.jinlia.show.modules.base.system.entity.Label;
import com.jinlia.show.modules.base.system.service.LabelMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 标签管理器
 */
@RestController
@RequestMapping("label")
public class LabelController {
    @Autowired
    private LabelMapperService labelMapperService;

    /**
     * 查询标签
     */
    @GetMapping("tree")
    public List<LabelVO> treeList() {
        List<LabelVO> list = labelMapperService.treeList();
        return list;
    }


    @PostMapping("/page")
    public ResponseEntity<IPage<Label>> getPage(@RequestBody LabelQuery labelQuery) {
        IPage<Label> page = labelMapperService.getPage(labelQuery);
        return ResponseEntity.ok(page);
    }

    /**
     * 新增标签
     *
     * @param label
     * @return
     */
    @PostMapping("add")
    public Label addLabel(@RequestBody Label label) {
        /**
         * 校验标签
         */
        String labelByValue = IBaseEnum.getLabelByValue(label.getLabel(), LabelEntityEnum.class);
        if (Objects.isNull(labelByValue)) {
            throw new MyRuntimeException("标签对象不存在!!!");
        }
        label = labelMapperService.addLabel(label);
        return label;
    }
    @PostMapping("update")
    public Label updateLabel(@RequestBody Label label) {
        /**
         * 校验标签
         */
        if (Objects.isNull(label.getId())) {
            throw new MyRuntimeException("标签不存在!!!");
        }
        label = labelMapperService.updateLabel(label);
        return label;
    }

    /**
     * 查询标签类型
     *
     * @param label
     * @return
     */
    @PostMapping("list/type")
    public List<Label> listLabelType(@RequestBody Label label) {
        if (StringUtils.isEmpty(label.getLabel())) {
            throw new MyRuntimeException("标签对象不能为空!!!");
        }
        List<Label> list = labelMapperService.listType(label.getLabel());
        return list;
    }

    /**
     * 查询标签名称
     *
     * @param label
     * @return
     */
    @PostMapping("list/name")
    public List<Label> listLabelName(@RequestBody Label label) {
        if (StringUtils.isEmpty(label.getLabel())) {
            throw new MyRuntimeException("标签对象和标签类型不能为空!!!");
        }
        List<Label> list = labelMapperService.listName(label.getLabel(), null);
        return list;
    }
}
